<% admin_breadcrumb(plural_resource_name(Spree::Shipment)) %>


<% manifest_items = Spree::ShippingManifest.new(inventory_units: shipment.inventory_units.where(carton_id: nil)).items %>

<div id="<%= "shipment_#{shipment.id}" %>" class="js-shipment-edit" data-hook="admin_shipment_form">
  <fieldset class="no-border-bottom">
    <legend align="center" class="stock-location" data-hook="stock-location">
      <span class="shipment-number"><%= shipment.number %></span>
      -
      <span class="shipment-state"><%= Spree.t("shipment_states.#{shipment.state}") %></span>
      <%= Spree.t(:package_from) %>
      <strong class="stock-location-name" data-hook="stock-location-name">'<%= shipment.stock_location.name %>'</strong>
    </legend>

    <% if shipment.ready? && can?(:ship, shipment) %>
      <%= form_tag("#", class: "admin-ship-shipment") do %>
        <%= check_box_tag :send_mailer, true, true %>
        <%= label_tag :send_mailer, Spree.t(:send_mailer) %>
        <%= submit_tag Spree.t('actions.ship'), class: "ship-shipment-button" %>
      <% end %>
    <% end %>
  </fieldset>

  <table class="stock-contents shipment index" data-hook="stock-contents">
    <colgroup>
      <col style="width: 10%;" />
      <col style="width: 30%;" />
      <col style="width: 15%;" />
      <col style="width: 15%;" />
      <col style="width: 15%;" />
      <col style="width: 15%;" />
    </colgroup>

    <thead>
      <tr>
        <th colspan="2"><%= Spree::LineItem.human_attribute_name(:description) %></th>
        <th><%= Spree::LineItem.human_attribute_name(:price) %></th>
        <th><%= Spree::LineItem.human_attribute_name(:quantity) %></th>
        <th><%= Spree::LineItem.human_attribute_name(:total) %></th>
        <th class="orders-actions actions" data-hook="admin_order_form_line_items_header_actions"></th>
      </tr>
    </thead>

    <tbody data-shipment-number="<%= shipment.number %>" data-order-number="<%= order.number %>">
      <%= render 'spree/admin/orders/shipment_manifest', { shipment_number: shipment.number, shipment_manifest: manifest_items } %>

      <% unless shipment.shipped? %>
        <tr class="edit-method hidden total">
          <td colspan="5">
            <div class="row">
              <div class="col-xs-4">
                <div class="field">
                  <%= label_tag 'selected_shipping_rate_id', Spree::ShippingMethod.model_name.human %>
                  <%= select_tag :selected_shipping_rate_id,
                        options_for_select(shipment.shipping_rates.map {|sr| ["#{sr.name} #{sr.display_price}", sr.id] }, shipment.selected_shipping_rate_id),
                        {:class => 'select2 fullwidth', :data => {'shipment-number' => shipment.number } } %>
                </div>
              </div>
            </div>
          </td>
          <td class="actions">
            <% if can? :update, shipment %>
              <%= link_to '', '#', :class => 'save-method fa fa-check no-text with-tip',
                :data => {'shipment-number' => shipment.number, :action => 'save'}, title: Spree.t('actions.save') %>
              <%= link_to '', '#', :class => 'cancel-method fa fa-cancel no-text with-tip',
                :data => {:action => 'cancel'}, :title => Spree.t('actions.cancel') %>
            <% end %>
          </td>
        </tr>
        <% end %>

        <tr class="show-method total">
          <% if rate = shipment.selected_shipping_rate %>
            <td colspan="4">
              <strong><%= rate.name %></strong>
            </td>
            <td class="total" align="center">
              <span><%= shipment.display_cost %></span>
            </td>
          <% else %>
            <td colspan='5'><%= Spree.t(:no_shipping_method_selected) %></td>
          <% end %>

          <td class="actions">
            <% if can?(:update, shipment) && !shipment.shipped? %>
              <%= link_to '', '#', :class => 'edit-method fa fa-edit no-text with-tip', :data => {:action => 'edit'}, :title => Spree.t('actions.edit') %>
            <% end %>
          </td>
        </tr>

      <tr class="edit-tracking hidden total">
        <td colspan="5">
          <label><%= Spree::Shipment.human_attribute_name(:tracking) %>:</label>
          <%= text_field_tag :tracking, shipment.tracking, id: nil %>
        </td>
        <td class="actions">
          <% if can? :update, shipment %>
            <%= link_to '', '#', :class => 'save-tracking fa fa-check no-text with-tip', :data => {'shipment-number' => shipment.number, :action => 'save'}, :title => Spree.t('actions.save') %>
            <%= link_to '', '#', :class => 'cancel-tracking fa fa-cancel no-text with-tip', :data => {:action => 'cancel'}, :title => Spree.t('actions.cancel') %>
          <% end %>
        </td>
      </tr>

      <% if order.special_instructions.present? %>
        <tr class='special_instructions'>
          <td colspan="5">
            <strong><%= Spree.t(:special_instructions) %>:&nbsp;</strong><%= order.special_instructions %>
          </td>
        </tr>
      <% end %>

      <tr class="show-tracking total">
        <td colspan="5" class="tracking-value">
          <% if shipment.tracking.present? %>
            <strong><%= Spree::Shipment.human_attribute_name(:tracking) %>:</strong> <%= shipment.tracking %>
          <% else %>
            <%= Spree.t(:no_tracking_present) %>
          <% end %>
        </td>
        <td class="actions">
          <% if can? :update, shipment %>
            <%= link_to '', '#', :class => 'edit-tracking fa fa-edit no-text with-tip', :data => {:action => 'edit'}, :title => Spree.t('actions.edit') %>
          <% end %>
        </td>
      </tr>
    </tbody>
  </table>
</div>
